package com.example.ai.controller;

import com.example.ai.service.AuthService;
import com.example.ai.service.CustomerServiceChatbot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 客服控制器，处理智能客服对话相关请求
 */
@RestController
@RequestMapping("/api/customer-service")
public class CustomerServiceController {
    
    private final CustomerServiceChatbot chatbot;
    private final AuthService authService;
    
    @Autowired
    public CustomerServiceController(CustomerServiceChatbot chatbot, AuthService authService) {
        this.chatbot = chatbot;
        this.authService = authService;
    }
    
    /**
     * 发送消息给智能客服
     * @param token 用户认证token
     * @param message 用户消息内容
     * @return 客服回复
     */
    @PostMapping("/chat")
    public String chatWithCustomerService(
            @RequestHeader("Authorization") String token,
            @RequestBody String message) {
        // 验证token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        
        if (!authService.validateToken(token)) {
            throw new RuntimeException("未授权访问，请先登录");
        }
        
        // 获取用户ID作为会话标识
        String userId = authService.getUserByToken(token).getUsername();
        
        // 调用智能客服处理消息
        return chatbot.handleCustomerQuery(userId, message);
    }
    
    /**
     * 清除会话历史
     * @param token 用户认证token
     * @return 操作结果消息
     */
    @DeleteMapping("/chat/history")
    public String clearChatHistory(@RequestHeader("Authorization") String token) {
        // 验证token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        
        if (!authService.validateToken(token)) {
            throw new RuntimeException("未授权访问，请先登录");
        }
        
        // 获取用户ID
        String userId = authService.getUserByToken(token).getUsername();
        
        // 清除会话历史
        chatbot.clearConversationHistory(userId);
        
        return "会话历史已清除";
    }
    
    /**
     * 获取商品信息的客服回答（专用接口）
     * @param token 用户认证token
     * @param productId 商品ID
     * @return 客服关于商品的回答
     */
    @GetMapping("/product-info/{productId}")
    public String getProductInfo(
            @RequestHeader("Authorization") String token,
            @PathVariable String productId) {
        // 验证token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        
        if (!authService.validateToken(token)) {
            throw new RuntimeException("未授权访问，请先登录");
        }
        
        // 获取商品信息的客服回答
        return chatbot.getProductInfoResponse(productId);
    }
    
    /**
     * 获取订单状态的客服回答（专用接口）
     * @param token 用户认证token
     * @param orderId 订单ID
     * @return 客服关于订单的回答
     */
    @GetMapping("/order-status/{orderId}")
    public String getOrderStatus(
            @RequestHeader("Authorization") String token,
            @PathVariable String orderId) {
        // 验证token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }
        
        if (!authService.validateToken(token)) {
            throw new RuntimeException("未授权访问，请先登录");
        }
        
        // 获取订单状态的客服回答
        return chatbot.getOrderStatusResponse(orderId);
    }
}